import 'dart:io';
import 'package:word_game/battle/battle.dart';
import 'package:word_game/engine/core.dart';
import 'package:yaml/yaml.dart';

import '../fetch_water_event.dart';
import '../new_monster_event.dart';
import '../search_thing_event.dart';

class EventableManager extends Object {
  static EventableManager instance = EventableManager();

  Map<String, Eventable> _objectMap={};

  appendObjectsFromYAML(String fileName) {
    var content = File(fileName).readAsStringSync();
    var yaml = loadYaml(content);
    var items = yaml['events'] as List;
    for (var item in items) {
      Eventable obj;
      var type = item['type'];
      if (type=='FetchWaterEvent') {
        obj = FetchWaterEvent();
      } else if (type=='NewMonsterEvent') {
        obj = NewMonsterEvent(item['name']);
      } else if (type=='SearchThingEvent') {
        obj = SearchThingEvent(item['thingCode']);
      } else {
        obj = Eventable(item['name']);
      }
      obj.code = item['code'];
      
      _objectMap[obj.code] = obj;
    }
  }

  Eventable objectWithCode(String code) {
    return _objectMap[code];
  }

}